﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace Cloudix.Common
{
    /// <summary>
    /// Provides a container for a collection of ContentBlocks and appropriate functionality
    /// to manage these.
    /// </summary>
    public class ContentCatalog
    {
        public ObservableCollection<ContentBlock> Items { get; set; }

        public ContentCatalog()
        {
            this.Items = new ObservableCollection<ContentBlock>();

            // Hook up to the ObservableCollection's event handlers
            this.Items.CollectionChanged += new NotifyCollectionChangedEventHandler(ContentCollectionChanged);
        }

        public virtual void ContentCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // Empty by default
        }

        public void Add(ContentBlock content)
        {
            if (content.Id == null || content.Id == Guid.Empty)
            {
                // It is an insert, so generate a new identifier
                content.Id = Guid.NewGuid();
            }
            else
            {
                // It is an update
                
            }
        }

        public bool Exists(Guid id)
        {
            return Exists(new ContentBlock() { Id = id });
        }

        public bool Exists(ContentBlock contentBlock)
        {
            var value = Items.Where(i => i.Id == contentBlock.Id).FirstOrDefault();
            if (value == null)
                return false;
            else
                return true;
        }
    }
}
